package com.aric.springbootmusic.service.impl;

import com.aric.springbootmusic.entity.pojo.Permission;
import com.aric.springbootmusic.entity.vo.BasePermissionVO;
import com.aric.springbootmusic.entity.vo.PermissionVO;
import com.aric.springbootmusic.entity.vo.RightVO;
import com.aric.springbootmusic.mapper.PermissionMapper;
import com.aric.springbootmusic.service.PermissionService;
import com.aric.springbootmusic.utils.Result;
import com.aric.springbootmusic.utils.ResultUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Transactional //开启事务
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    //查询所有的权限，树形排列
    @Override
    public Result findAllPermission() {
        List<Permission>  permissionList=permissionMapper.selectAllPermission();
        if(Objects.nonNull(permissionList)){
            List<PermissionVO> listF=new ArrayList<>();
            permissionList.forEach(authorF->{
                List<Permission> authorSs = authorF.getChildren();
                List<PermissionVO> listS=new ArrayList<>();
                authorSs.forEach(authorS->{
                    List<Permission> authorTs = authorS.getChildren();
                    List<PermissionVO> listT=new ArrayList<>();
                    authorTs.forEach(authorT->{
                        //三级权限
                        PermissionVO permissionVO = PermissionVO.builder()
                                .id(authorT.getPsId())
                                .AuthorName(authorT.getPsCname())
                                .build();
                        listT.add(permissionVO);
                    });

                    //二级权限
                    PermissionVO vo = PermissionVO.builder()
                            .id(authorS.getPsId())
                            .AuthorName(authorS.getPsCname())
                            .children(listT)
                            .build();
                    listS.add(vo);
                });

                //一级权限
                PermissionVO vo = PermissionVO.builder()
                        .id(authorF.getPsId())
                        .AuthorName(authorF.getPsCname())
                        .children(listS)
                        .build();
                listF.add(vo);
            });
            return ResultUtil.getResponseResult(HttpStatus.OK.value(), "查询权限成功!",listF);
        }
        return ResultUtil.getResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "查询权限失败!",null);
    }

    @Override
    public Result findRoleAuthor(Integer id) {
        List<Integer> ids=permissionMapper.selectRoleAuthor(id);
        if(Objects.nonNull(ids)){
            return ResultUtil.getResponseResult(HttpStatus.OK.value(), "查询角色权限成功!",ids);
        }
        return ResultUtil.getResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "查询角色权限失败!",null);
    }

    @Override
    public Result findAllRight() {
        List<Permission> permissions = permissionMapper.selectList(null);
        if(Objects.nonNull(permissions)){
            List<RightVO> rightVOList=new ArrayList<>();
            permissions.forEach(permission -> {
                RightVO rightVO = RightVO.builder()
                        .authName(permission.getPsCname())
                        .level(permission.getPsLevel().getLevelCode())
                        .build();
                rightVOList.add(rightVO);
            });

            return ResultUtil.getResponseResult(HttpStatus.OK.value(), "查询权限成功!",rightVOList);
        }
        return ResultUtil.getResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "查询权限失败!",null);
    }
}
